﻿using AnesSystem.Models;
using AnesSystem.Services;
using Common;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    class ConfigContainersCtrlViewModel : BaseViewModel<ConfigContainersCtrlViewModel>
    {
        #region Variables

        private string _seachValue = "";
        private ObservableCollection<ConfigContainersModel> _displayConfigContainersModels = new ObservableCollection<ConfigContainersModel>();

        #endregion

        #region Constructor

        public ConfigContainersCtrlViewModel()
        {
            //ConfigContainersDataGridViewModel.Instance.ConfigContainersModel.ToList().Where(O => O.Name.Contains(_seachValue) || O.Code.Contains(_seachValue)).ToList();
        }

        #endregion

        #region Properties

        public ObservableCollection<ConfigContainersModel> DisplayConfigContainersModels
        {
            get
            {
                _displayConfigContainersModels.Clear();
                List<ConfigContainersModel> lst= ConfigContainersDataGridViewModel.Instance.ConfigContainersModel.ToList().Where(O => O.Name.Contains(_seachValue)).ToList();
                foreach (var item in lst)
                {

                    item.PropertyChanged += Item_PropertyChanged;
                    _displayConfigContainersModels.Add(item);
                }
                return _displayConfigContainersModels;
            }

            set
            {
                ConfigContainersDataGridViewModel.Instance.ConfigContainersModel = value;
                RaisePropertyChanged("DisplayConfigDepartmentsModels");
            }
        }

      
        public string SeachValue
        {
            get
            {
                return _seachValue;
            }

            set
            {
                _seachValue = value;
                RaisePropertyChanged("DisplayConfigContainersModels");
            }
        }
        #endregion

        #region Event

        #endregion

        #region Private Methods
        private void AddConfigContainersModel(ConfigContainersModel obj)
        {
            if (obj.Id != new ObjectId("000000000000000000000000"))
            {

                ConfigContainersModelService.Instance.Update(obj);
                ConfigContainersDataGridViewModel.Instance.ConfigContainersModel.Add(obj);
            }
            else ConfigContainersModelService.Instance.Insert(obj);


        }
        private void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            ConfigContainersModelService.Instance.Update(sender as ConfigContainersModel);
        }

        #endregion

        #region Public/Protected Methods
        public void DelConfigContainersModel(ConfigContainersModel configContainersModel)
        {
            if (configContainersModel != null)
            {
                ConfigContainersDataGridViewModel.Instance.ConfigContainersModel.Remove(configContainersModel);
                RaisePropertyChanged("DisplayConfigContainersModels");
                ConfigContainersModelService.Instance.DeleteById(configContainersModel.Id);
            }
        }

        public void AddRow()
        {
            ConfigContainersModel model = new ConfigContainersModel();
            DisplayConfigContainersModels.Insert(0, model);
            model.PropertyChanged += Model_PropertyChanged;
        }
        #endregion

        #region EventHandlers
        private void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            AddConfigContainersModel(sender as ConfigContainersModel);
        }

        #endregion

        #region Command

        #region ChangeDoctorCommand

        #endregion

        #region OpenPopupCommand

        #endregion
        #endregion
    }
}
